﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Drawing.Printing;
using System.Windows.Forms;
using SVC.client;
using DevExpress.XtraEditors;
namespace UI
{
    public static class my
    {
        private static ServiceClient bwserver;

        public static ServiceClient bw
        {
            get
            {


                if (bwserver == null)
                {
                    pwopenLink();
                }
                if (bwserver.State != System.ServiceModel.CommunicationState.Opened)
                {
                    pwopenLink();
                }

                return my.bwserver;

            }

        }

        public static string pwpid
        {
            get
            {
                if (bwserver == null)
                {
                    pwopenLink();
                }
                if (bwserver.State != System.ServiceModel.CommunicationState.Opened)
                {
                    pwopenLink();
                }
                try
                {
                    return bw.link("pid",AssemblyInfo.Version);
                }
                catch
                {

                    return null;
                }
            }

        }

        /// <summary>
        /// 打开连接
        /// </summary>
        public static void pwopenLink()
        {
            try
            {
                bwserver = new ServiceClient("NetTcpBinding_IService", MyConfig.getAppValue("Server"));
                bwserver.Open();
            }
            catch
            {

            }
        }

        /// <summary>
        /// 显示打印机设置窗口
        /// </summary>
        public static void showSetPrint()
        {
            XtraMessageBox.Show("系统未设置默认打印机，请先设置默认打印机再点击打印按钮！", "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            Forms.FrmPrintSet frm = new Forms.FrmPrintSet();
            frm.ShowDialog();
        }

        /// <summary>
        /// 检测打印机
        /// </summary>
        public static void chekPrint()
        {
            bool has = false;
            if (!string.IsNullOrEmpty(my.myprint))
            {
                foreach (string print in PrinterSettings.InstalledPrinters)
                {
                    if (print == my.myprint)
                    {
                        has = true;
                        break;
                    }
                }
                if (!has)
                {
                    my.myprint = "";
                    MyConfig.setAppValue("curPrint", "");
                    XtraMessageBox.Show("未找到系统设置的默认打印机，设置已被清除！\r\n请登录后重新设置系统默认打印机。", "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                }
            }

        }

        /// <summary>
        /// 检测模块权限
        /// </summary>
        /// <param name="pid">模块ID</param>
        /// <returns></returns>
        //public static bool checkpower(int pid)
        //{
        //    if (my.mypowertree == null)
        //    {
        //        return false;
        //    }
        //    if (my.mypowertree.MyPowers.Count(p => p.PID == pid) > 0)
        //    {
        //        return true;
        //    }
        //    return false;
        //}

        /// <summary>
        /// 检测模块权限
        /// </summary>
        /// <param name="pid">模块ID</param>
        /// <returns></returns>
        //public static BarItemVisibility checkpowerV(int pid)
        //{
        //    if (my.mypowertree == null)
        //    {
        //        return BarItemVisibility.Never;
        //    }
        //    if (my.mypowertree.MyPowers.Count(p => p.PID == pid) > 0)
        //    {
        //        return BarItemVisibility.Always;
        //    }
        //    return BarItemVisibility.Never;
        //}

        public static string appName { get; set; }
        public static string corName { get; set; }
        public static View_Emp_lite emp_lite { get; set; }
        public static DateTime logintime { get; set; }
        public static Forms.MainForm mainform { get; set; }
        public static int ACID { get; set; }
        public static int qdcount = 0;
        public static int wycount = 0;
        public static string myprint = MyConfig.getAppValue("curPrint");
        public static BindingList<dtype> dts = set_dts();

        private static BindingList<dtype> set_dts()
        {
           BindingList<dtype> ds = new BindingList<dtype>();
            DateTime dt = DateTime.Now.Date;

            dtype 无 = new dtype();
            无.id = -1;
            无.title = "无";

            ds.Add(无);

            dtype 昨天 = new dtype();
            昨天.id = 0;
            昨天.title = "昨天";
            昨天.sd = dt.AddDays(-1);
            昨天.ed = 昨天.sd.Value.AddDays(1).AddSeconds(-1);
            ds.Add(昨天);



            dtype 今天 = new dtype();
            今天.id = 1;
            今天.title = "今天";
            今天.sd = dt;
            今天.ed = dt.AddDays(1).AddSeconds(-1);
            ds.Add(今天);



            dtype 上周 = new dtype();
            上周.id = 2;
            上周.title = "上周";
            DateTime dt2 = dt.AddDays(-7);  //当前时间
            int dayOfWeek2 = Convert.ToInt32(dt2.DayOfWeek.ToString("d"));
            上周.sd = dt2.AddDays(1 - ((dayOfWeek2 == 0) ? 7 : dayOfWeek2));   //上周周一
            上周.ed = 上周.sd.Value.AddDays(7).AddSeconds(-1);  //上周周末
            ds.Add(上周);

            dtype 本周 = new dtype();
            本周.id = 3;
            本周.title = "本周";
            int dayOfWeek3 = Convert.ToInt32(dt.DayOfWeek.ToString("d"));
            本周.sd = dt.AddDays(1 - ((dayOfWeek3 == 0) ? 7 : dayOfWeek3));
            本周.ed = 本周.sd.Value.AddDays(7).AddSeconds(-1);
            ds.Add(本周);

            dtype 上月 = new dtype();
            上月.id = 4;
            上月.title = "上月";
            DateTime dt4 = dt.AddMonths(-1);
            上月.sd = Convert.ToDateTime(dt4.Year + "-" + dt4.Month + "-1");
            上月.ed = 上月.sd.Value.AddMonths(1).AddSeconds(-1); ;
            ds.Add(上月);

            dtype 本月 = new dtype();
            本月.id = 5;
            本月.title = "本月";
            本月.sd = Convert.ToDateTime(dt.Year + "-" + dt.Month + "-1");
            本月.ed = 本月.sd.Value.AddMonths(1).AddSeconds(-1);
            ds.Add(本月);

            for (int i = 1; i <= dt.Month; i++)
            {
                dtype d = new dtype();
                d.id = 5 + i;
                d.title = string.Format("{0}年{1}月", dt.Year, i);
                d.sd = Convert.ToDateTime(dt.Year.ToString() + "-" + i);
                d.ed = d.sd.Value.AddMonths(1).AddSeconds(-1);
                ds.Add(d);
            }
            return ds;
        }

        public static BindingList<phone_state> phone_state_List = set_phone_state_List();

        private static BindingList<phone_state> set_phone_state_List()
        {

                BindingList<phone_state> list = new BindingList<phone_state>();
                phone_state s0 = new phone_state();
                s0.id = 0;
                s0.memo = "未使用";
                list.Add(s0);
                phone_state s1 = new phone_state();
                s1.id = 1;
                s1.memo = "已使用";
                list.Add(s1);
                phone_state s2 = new phone_state();
                s2.id = 2;
                s2.memo = "停机";
                list.Add(s2);
                return list;
            
        }
    }
    public class dtype
    {
        public int id { get; set; }
        public string title { get; set; }
        public DateTime? sd { get; set; }
        public DateTime? ed { get; set; }
    }
}
